import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/utils/dialog_util.dart';
import 'package:oramemo/core/models/api_response.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/base_entity.dart';
import 'package:oramemo/entity/folder_query_entity.dart';
import 'package:oramemo/entity/requst/folder_put_entity.dart';
import 'package:oramemo/entity/requst/voice_moveto_entity.dart';
import 'package:oramemo/entity/user_white_entity.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/pages/user/management/models/user.dart';

class TranscriptionFilesLogic extends GetxController {
  final RxList<FolderQueryData> list = <FolderQueryData>[].obs;

  var defaultFolder = FolderQueryData()
    ..folderName = TextKey.folderNameAllFiles.tr
    ..id = '';
  late final Rx<FolderQueryData> currentFolder = defaultFolder.obs;

  @override
  void onInit() {
    loadFolders();
    super.onInit();
  }

  // 加载列表
  Future<void> loadFolders() async {
    try {
      final res =
          await DefaultApi().oraVoiceFolderQueryGet<FolderQueryEntity>();
      list.value = res?.data ?? <FolderQueryData>[];
      update();
    } catch (e) {
      TipUtil.showError(TextKey.userManagementLoadError.tr);
    }
  }

  // 添加
  Future<void> addFolder({required String name}) async {
    try {
      final res = await DefaultApi().oraVoiceFolderAddPost<BaseEntity>(
          folderName: name, isShowLoading: true);
      if (res?.isSuccess == true) {
        loadFolders();
        await TipUtil.showSuccess(TextKey.addSuccess.tr);
        Get.back();
      } else {
        TipUtil.showError(res?.msg ?? TextKey.addFailure.tr);
      }
    } catch (e) {
      TipUtil.showError(TextKey.addFailure.tr);
    }
  }

  // 编辑
  Future<void> editFolder({required FolderPutEntity entity}) async {
    try {
      final res = await DefaultApi()
          .oraVoiceFolderPut<BaseEntity>(body: entity, isShowLoading: true);
      if (res?.isSuccess == true) {
        final index = list.indexWhere((element) => element.id == entity.id);
        if (index != -1) {
          list[index].folderName = entity.folderName;
          update();
          await TipUtil.showSuccess(TextKey.editSuccess.tr);
          Get.back();
        }
      }
    } catch (e) {
      TipUtil.showError(TextKey.editFailure.tr);
    }
  }

  // 删除
  Future<void> deleteFolder(String ids) async {
    try {
      final res = await DefaultApi()
          .oraVoiceFolderIdsDelete<BaseEntity>(ids: ids, isShowLoading: true);
      if (res?.isSuccess == true) {
        list.removeWhere((model) => model.id == ids);
        update();
        TipUtil.showSuccess(TextKey.deleteSuccess.tr);
      }
    } catch (e) {
      TipUtil.showError(TextKey.deleteFailure.tr);
    }
  }

  Future<bool> moveToFolder({required String voiceId, required String folderId}) async {
    try {
      final res = await DefaultApi().oraVoiceRecordMoviePost<BaseEntity>(
          body: VoiceMovetoEntity()
            ..id = voiceId
            ..voiceFolderId = folderId,
          isShowLoading: true);
      if (res?.isSuccess == true) {
        update();
        Toast.show(TextKey.moveSuccess.tr);
        return true;
      }
    } catch (e) {
      Toast.show(TextKey.moveFailure.tr);
      return false;
    }
    return false;
  }
}
